//
//  HYFComposePictureView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/27.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//

import UIKit

//cell可重用标识符
private let HYFComposePictureViewId = "HYFComposePictureViewId"

class HYFComposePictureView: UICollectionView {
    
    //定义一个闭包,弹出相册
    var closure:(()->())?
    
    //保存图片数组
    var imageList:[UIImage] = [UIImage]()

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func addImages(image:UIImage){
        
        //添加图片
        imageList.append(image)
        
        //显示配图
        hidden = false
        //刷新UI
        reloadData()
    }
    
    
    func setupUI(){
        
        backgroundColor = UIColor.whiteColor()
        //首次创建的时候,不显示
        hidden = true
        
        let cellMargin:CGFloat = 5
        let cellWH = (SCREENW - 20 - 2*cellMargin)/3
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        //设置item
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        
        //设置边距
        layout.minimumLineSpacing = cellMargin
        layout.minimumInteritemSpacing = cellMargin
        
        //设置数据源代理
        dataSource = self
        delegate = self
        
        //注册 cell
        registerClass(HYFComposePictureViewCell.self, forCellWithReuseIdentifier: HYFComposePictureViewId)
        
    }
}

extension HYFComposePictureView:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        let count = imageList.count
        //当 0 或 9张图片 根本不显示 加号图片
        if  count == 0 || count == 9 {
            
            return count
        }
        
        return imageList.count + 1
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HYFComposePictureViewId, forIndexPath: indexPath) as! HYFComposePictureViewCell
        
        //判断如果是最后一个 item ,代表要显示加号图片
        if indexPath.item == imageList.count{
            
            cell.image = nil
            
        }else{
            
            cell.image = imageList[indexPath.item]
            
        }
        
        //赋值闭包
        cell.closure = {[weak self] in
            
            //移除指定位置的 image
            self?.imageList.removeAtIndex(indexPath.item)
            
            //个数为0 的时候,隐藏 视图
            if  self?.imageList.count == 0 {
                
                self?.hidden = true
            }
            //刷新界面
            self?.reloadData()
        }
        
        return cell
    }
    
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        //点击加号按钮时,执行闭包
        if indexPath.item == imageList.count{
            
            closure?()
            print("closure")
        }
        
        collectionView.deselectItemAtIndexPath(indexPath, animated: true)
    }
}


class HYFComposePictureViewCell:UICollectionViewCell{
    
    //定义一个闭包
    var closure:(()->())?
    
    //定义一个属性供外界赋值
    var image:UIImage?{
        
        didSet{
            
            //添加加号图片
            if image == nil{
                
                bgImageView.image = UIImage(named: "compose_pic_add")
                //设置高亮图片
                bgImageView.highlightedImage = UIImage(named: "compose_pic_add_highlighted")
                
             //隐藏删除按钮
                deleteButton.hidden = true
                
            }else{
                
                bgImageView.image = image
                bgImageView.highlightedImage = nil
                
                deleteButton.hidden = false
            }
        }
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //监听方法
    func buttonClick(){
        
        closure?()
      
    }
    
    
    func setupUI(){
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(deleteButton)
        
        deleteButton.snp_makeConstraints { (make) in
            
            make.top.trailing.equalTo(contentView)
            
        }
        
        //设置约束
        bgImageView.snp_makeConstraints { (make) in
            
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        
    }
    
    //懒加载控件
    private lazy var bgImageView:UIImageView = UIImageView()
    
    //懒加载删除按钮
    private lazy var deleteButton:UIButton = {
        
       let button = UIButton()
        //添加点击事件
        button.addTarget(self, action: #selector(HYFComposePictureViewCell.buttonClick), forControlEvents: .TouchUpInside)
       
        button.setImage( UIImage(named: "compose_photo_close"), forState: .Normal)
 
        return button
    }()
    
}








